﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MAT.Model;
using System.Windows.Forms;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;
using System.Xml.Linq;

namespace MAT.Controllers
{
    class ctrAddItem1
    {
        private frmAddItem1 _frmAddItem1 = null;
        public frmAddItem1 FrmAddItem1
        {
            get { return _frmAddItem1; }
            set { _frmAddItem1 = value; }
        }
        string tenanh;
        MAT_Context context = new MAT_Context();
        CommonClass cl = new CommonClass();
        public void btnReset_Click()
        {
            _frmAddItem1.cmbSupplier.Text = "";
            _frmAddItem1.txtAssetID.Text = "";
            _frmAddItem1.txtAssetName.Text = "";
            _frmAddItem1.cmbCategory.Text = "";
            _frmAddItem1.txtBarcode.Text = "";
            _frmAddItem1.txtBrand.Text = "";
            _frmAddItem1.txtModelNum.Text = "";
            _frmAddItem1.txtPrice.Text = "";

            frmAddItem1_Load();
        }
        public void btnReset1_Click()
        {
            _frmAddItem1.txtColor.Text = "";
            _frmAddItem1.txtChipset.Text = "";
            _frmAddItem1.txtGraphic.Text = "";
            _frmAddItem1.txtHDD.Text = "";
            _frmAddItem1.txtHeight.Text = "";
            _frmAddItem1.txtMaterial.Text = "";
            _frmAddItem1.txtOther.Text = "";
            _frmAddItem1.txtPower.Text = "";
            _frmAddItem1.txtRam.Text = "";
            _frmAddItem1.txtShape.Text = "";
            _frmAddItem1.txtSize.Text = "";
            _frmAddItem1.txtType.Text = "";
            _frmAddItem1.txtWeight.Text = "";
        }
        public void dtpAquiredDate_ValueChanged()
        {
            _frmAddItem1.dtpExpiredDate.MinDate = _frmAddItem1.dtpAquiredDate.Value;
        }
        public void InsertOnSubmit(MATContext entity) { }
        public void frmAddItem1_Load()
        {
            List<SUPPLIER> supplier = context.SUPPLIERs.ToList();
            _frmAddItem1.cmbSupplier.DisplayMember = "supplier_name";
            _frmAddItem1.cmbSupplier.ValueMember = "supplier_id";
            _frmAddItem1.cmbSupplier.DataSource = supplier;
            // _frmAddItem1.txtAssetID.ReadOnly = true;
            _frmAddItem1.txtBarcode.ReadOnly = true;
            List<CATEGORY> categories = context.CATEGORies.ToList();
            _frmAddItem1.cmbCategory.DisplayMember = "Category_name";
            _frmAddItem1.cmbCategory.ValueMember = "Category_id";
            _frmAddItem1.cmbCategory.DataSource = categories;
            List<ROOM> Rooms = context.ROOMs.ToList();
            _frmAddItem1.cmbRoom.DisplayMember = "Room_name";
            _frmAddItem1.cmbRoom.ValueMember = "Room_ID";
            _frmAddItem1.cmbRoom.DataSource = Rooms;
        }
        //public void cmbSupplier_SelectedIndexChanged()
        //{
        //    List<SUPPLIER> supplier = context.SUPPLIERs.Where(s => s.SUPPLIER_ID == (string)_frmAddItem1.cmbSupplier.SelectedValue).ToList();
        //    foreach (SUPPLIER sup in supplier)
        //    {
        //        List<ASSET> assets = context.ASSETs.Where(a => a.SUPPLIER_ID == sup.SUPPLIER_ID).ToList();
        //        List<CATEGORY> category = new List<CATEGORY>();
        //        foreach (ASSET asset in assets)
        //        {
        //            category.Add(asset.CATEGORY);
        //            _frmAddItem1.cmbCategory.DataSource = category;
        //            _frmAddItem1.cmbCategory.DisplayMember = "category_name";
        //            _frmAddItem1.cmbCategory.ValueMember = "category_id";
        //        }
        //    }
        //}
        public void cmbRoom_SelectedIndexChanged()
        {
            _frmAddItem1.cmbOwner.DataSource = null;

            ROOM room = context.ROOMs.FirstOrDefault(r => r.ROOM_ID == (string)_frmAddItem1.cmbRoom.SelectedValue);
            if (room != null)
            {
                List<OWNER> owner = room.OWNERS.ToList();
                _frmAddItem1.cmbOwner.DataSource = owner;
                _frmAddItem1.cmbOwner.DisplayMember = "employee_name";
                _frmAddItem1.cmbOwner.ValueMember = "employee_id";

            }
        }
        
        public void btnSave_Click()
        {
            try
            {
                DialogResult result;
                result = MessageBox.Show("Are you sure you want to add this asset?", "Add Asset", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                ASSET assets = new ASSET();
                if (result == DialogResult.Yes)
                {
                    assets.ASSET_ID = _frmAddItem1.txtAssetID.Text;
                    assets.CATEGORY_ID = _frmAddItem1.cmbCategory.SelectedValue.ToString();
                    assets.MODEL_NUM = _frmAddItem1.txtModelNum.Text;
                    assets.BRAND = _frmAddItem1.txtBrand.Text;
                    assets.ACQUIRED_DATE = _frmAddItem1.dtpAquiredDate.Value;
                    assets.EXPIRED_DATE = _frmAddItem1.dtpExpiredDate.Value;
                    assets.BARCODE = _frmAddItem1.txtBarcode.Text;
                    assets.PRICE = Convert.ToInt32(_frmAddItem1.txtPrice.Text);
                    assets.SUPPLIER_ID = _frmAddItem1.cmbSupplier.SelectedValue.ToString();
                    assets.ASSET_NAME = _frmAddItem1.txtAssetName.Text;
                    XMLWriter();
                    //Save Image
                    MemoryStream stream = new MemoryStream();
                    _frmAddItem1.ptbAssetImage.Image.Save(stream, ImageFormat.Jpeg);
                    assets.IMAGE = stream.ToArray();

                    btnReset_Click();
                    btnReset1_Click();
                    context.ASSETs.AddObject(assets);
                    context.SaveChanges();
                    MessageBox.Show("Successful!");
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Please add piture for asset!");
            }
        }
        public void XMLWriter()
        {
            switch (_frmAddItem1.cmbCategory.Text)
            {
                case "CPU":
                    CPU(_frmAddItem1.txtAssetID.Text, _frmAddItem1.txtHDD.Text, _frmAddItem1.txtRam.Text, _frmAddItem1.txtChipset.Text, _frmAddItem1.txtGraphic.Text, _frmAddItem1.txtPower.Text, _frmAddItem1.txtOther.Text);
                    break;
                case "TELEVISION":
                    Television(_frmAddItem1.txtAssetID.Text, _frmAddItem1.txtColor.Text, _frmAddItem1.txtType.Text, _frmAddItem1.txtSize.Text, _frmAddItem1.txtPower.Text,_frmAddItem1.txtOther.Text);
                    break;
                case "AIR-CONDITIONER":
                    AirConditioner(_frmAddItem1.txtAssetID.Text, _frmAddItem1.txtColor.Text, _frmAddItem1.txtSize.Text, _frmAddItem1.txtPower.Text, _frmAddItem1.txtWeight.Text, _frmAddItem1.txtOther.Text);
                    break;
                case"MONITOR":
                    Monitor(_frmAddItem1.txtAssetID.Text, _frmAddItem1.txtColor.Text, _frmAddItem1.txtType.Text, _frmAddItem1.txtSize.Text, _frmAddItem1.txtOther.Text);
                    break;
                case "CHAIR":
                    Chair(_frmAddItem1.txtAssetID.Text, _frmAddItem1.txtColor.Text, _frmAddItem1.txtType.Text, _frmAddItem1.txtHeight.Text, _frmAddItem1.txtMaterial.Text, _frmAddItem1.txtOther.Text);
                    break;
                case "DESK":
                    Desk(_frmAddItem1.txtAssetID.Text, _frmAddItem1.txtColor.Text, _frmAddItem1.txtType.Text, _frmAddItem1.txtShape.Text, _frmAddItem1.txtMaterial.Text, _frmAddItem1.txtHeight.Text, _frmAddItem1.txtOther.Text);
                    break;
            }
        }
        public void btnLoadImage_Click()
        {
            try
            {
                FileDialog fdg = new OpenFileDialog();
                fdg.InitialDirectory = @"d:\";
                fdg.Filter = "File anh(*.jpg;*.bmp;*.gif)|*jpg;*bmp;*gif";
                if (fdg.ShowDialog() == DialogResult.OK)
                {
                    tenanh = fdg.FileName;
                    Bitmap anh = new Bitmap(tenanh);
                    _frmAddItem1.ptbAssetImage.SizeMode = PictureBoxSizeMode.Zoom;
                    _frmAddItem1.ptbAssetImage.Image = (Image)anh;
                }
                fdg = null;
            }
            catch (Exception ex)
            {
                tenanh = " ";
                MessageBox.Show(ex.Message);
            }
        }
        public void ptbAssetImage_MouseClick_1()
        {
            try
            {
                FileDialog fdg = new OpenFileDialog();
                fdg.InitialDirectory = @"d:\";
                fdg.Filter = "File anh(*.jpg;*.bmp;*.gif)|*jpg;*bmp;*gif";
                if (fdg.ShowDialog() == DialogResult.OK)
                {
                    tenanh = fdg.FileName;
                    Bitmap anh = new Bitmap(tenanh);
                    _frmAddItem1.ptbAssetImage.SizeMode = PictureBoxSizeMode.Zoom;
                    _frmAddItem1.ptbAssetImage.Image = (Image)anh;
                }
                fdg = null;
            }
            catch (Exception ex)
            {
                tenanh = " ";
                MessageBox.Show(ex.Message);
            }
        }
        //Load Image
        public void pictureBox1_MouseClick()
        {
            try
            {
                FileDialog fdg = new OpenFileDialog();
                fdg.InitialDirectory = @"d:\";
                fdg.Filter = "File anh(*.jpg;*.bmp;*.gif)|*jpg;*bmp;*gif";
                if (fdg.ShowDialog() == DialogResult.OK)
                {
                    tenanh = fdg.FileName;
                    Bitmap anh = new Bitmap(tenanh);
                    _frmAddItem1.ptbAssetImage.SizeMode = PictureBoxSizeMode.Zoom;
                    _frmAddItem1.ptbAssetImage.Image = (Image)anh;
                }
                fdg = null;
            }
            catch (Exception ex)
            {
                tenanh = " ";
                MessageBox.Show(ex.Message);
            }

        }
        public void btnClear_Click()
        {
            _frmAddItem1.ptbAssetImage.Image = null;
        }
        public void btnClose_Click()
        {
            _frmAddItem1.Close();
        }
        public static void CPU(string AssetID, string HDD, string Ram, string Chipset, string Graphic, string Power, string Other)
        {
            //initiate nodes with father node (MoveHistory) and child node(AssetName, Date, OldRoom)
            var MAT = new XElement("Asset", new XElement("AssetID", AssetID), new XElement("HDD", HDD), new XElement("Ram", Ram), new XElement("Chipset", Chipset), new XElement("Graphic", Graphic), new XElement("Power", Power), new XElement("Other",Other) );
            var doc = new XDocument();
            //create xml file.
            if (File.Exists(@"..\..\XML\Asset.xml"))
            {
                doc = XDocument.Load(@"..\..\XML\Asset.xml");
                doc.Element("MAT").Add(MAT);//grandfather node (MAT)
            }
            else
            {
                doc = new XDocument(new XElement("MAT", MAT));
            }
            doc.Save(@"..\..\XML\Asset.xml");
        }
        public static void Television(string AssetID, string Color, string Type, string Size,string Power, string Other)
        {
            //initiate nodes with father node (MoveHistory) and child node(AssetName, Date, OldRoom)
            var MAT = new XElement("Asset", new XElement("AssetID", AssetID), new XElement("Color", Color), new XElement("type", Type), new XElement("Size", Size),new XElement("Power",Power), new XElement("Other", Other));
            var doc = new XDocument();
            //create xml file.
            if (File.Exists(@"..\..\XML\Asset.xml"))
            {
                doc = XDocument.Load(@"..\..\XML\Asset.xml");
                doc.Element("MAT").Add(MAT);//grandfather node (MAT)
            }
            else
            {
                doc = new XDocument(new XElement("MAT", MAT));
            }
            doc.Save(@"..\..\XML\Asset.xml");
        }
        public static void AirConditioner(string AssetID, string Color, string Size, string Power, string Weight, string Other)
        {
            //initiate nodes with father node (MoveHistory) and child node(AssetName, Date, OldRoom)
            var MAT = new XElement("Asset", new XElement("AssetID", AssetID), new XElement("Color", Color), new XElement("Size", Size), new XElement("Power", Power), new XElement("Weight", Weight), new XElement("Other", Other));
            var doc = new XDocument();
            //create xml file.
            if (File.Exists(@"..\..\XML\Asset.xml"))
            {
                doc = XDocument.Load(@"..\..\XML\Asset.xml");
                doc.Element("MAT").Add(MAT);//grandfather node (MAT)
            }
            else
            {
                doc = new XDocument(new XElement("MAT", MAT));
            }
            doc.Save(@"..\..\XML\Asset.xml");
        }
        public static void Monitor(string AssetID, string Color, string Type, string Size, string Other)
        {
            //initiate nodes with father node (MoveHistory) and child node(AssetName, Date, OldRoom)
            var MAT = new XElement("Asset", new XElement("AssetID", AssetID), new XElement("Color", Color), new XElement("type", Type), new XElement("Size", Size), new XElement("Other", Other));
            var doc = new XDocument();
            //create xml file.
            if (File.Exists(@"..\..\XML\Asset.xml"))
            {
                doc = XDocument.Load(@"..\..\XML\Asset.xml");
                doc.Element("MAT").Add(MAT);//grandfather node (MAT)
            }
            else
            {
                doc = new XDocument(new XElement("MAT", MAT));
            }
            doc.Save(@"..\..\XML\Asset.xml");
        }
        public static void Chair(string AssetID, string Color, string Type, string Height, string Material,string Other)
        {
            //initiate nodes with father node (MoveHistory) and child node(AssetName, Date, OldRoom)
            var MAT = new XElement("Asset", new XElement("AssetID", AssetID), new XElement("Color", Color), new XElement("type", Type), new XElement("height", Height), new XElement("Material", Material), new XElement("Other", Other));
            var doc = new XDocument();
            //create xml file.
            if (File.Exists(@"..\..\XML\Asset.xml"))
            {
                doc = XDocument.Load(@"..\..\XML\Asset.xml");
                doc.Element("MAT").Add(MAT);//grandfather node (MAT)
            }
            else
            {
                doc = new XDocument(new XElement("MAT", MAT));
            }
            doc.Save(@"..\..\XML\Asset.xml");
        }
        public static void Desk(string AssetID, string Color, string Type, string Shape, string Material, string Height, string Other)
        {
            //initiate nodes with father node (MoveHistory) and child node(AssetName, Date, OldRoom)
            var MAT = new XElement("Asset", new XElement("AssetID", AssetID), new XElement("Color", Color), new XElement("type", Type), new XElement("Shape", Shape), new XElement("Material", Material), new XElement("Height", Height), new XElement("Other", Other));
            var doc = new XDocument();
            //create xml file.
            if (File.Exists(@"..\..\XML\Asset.xml"))
            {
                doc = XDocument.Load(@"..\..\XML\Asset.xml");
                doc.Element("MAT").Add(MAT);//grandfather node (MAT)
            }
            else
            {
                doc = new XDocument(new XElement("MAT", MAT));
            }
            doc.Save(@"..\..\XML\Asset.xml");
        }
        public void cmbCategory_SelectedIndexChanged()
        {
            switch (_frmAddItem1.cmbCategory.Text)
            {
                case "DESK":
                    _frmAddItem1.txtColor.Enabled = true;
                    _frmAddItem1.txtChipset.Enabled = false;
                    _frmAddItem1.txtGraphic.Enabled = false;
                    _frmAddItem1.txtHDD.Enabled = false;
                    _frmAddItem1.txtHeight.Enabled = true;
                    _frmAddItem1.txtMaterial.Enabled = true;
                    _frmAddItem1.txtOther.Enabled = true;
                    _frmAddItem1.txtPower.Enabled = false;
                    _frmAddItem1.txtRam.Enabled = false;
                    _frmAddItem1.txtShape.Enabled = true;
                    _frmAddItem1.txtSize.Enabled = false;
                    _frmAddItem1.txtType.Enabled = true;
                    _frmAddItem1.txtWeight.Enabled = false;
                    break;
                case "CHAIR":
                    _frmAddItem1.txtColor.Enabled = true;
                    _frmAddItem1.txtChipset.Enabled = false;
                    _frmAddItem1.txtGraphic.Enabled = false;
                    _frmAddItem1.txtHDD.Enabled = false;
                    _frmAddItem1.txtHeight.Enabled = false;
                    _frmAddItem1.txtMaterial.Enabled = true;
                    _frmAddItem1.txtOther.Enabled = true;
                    _frmAddItem1.txtPower.Enabled = false;
                    _frmAddItem1.txtRam.Enabled = false;
                    _frmAddItem1.txtShape.Enabled = false;
                    _frmAddItem1.txtSize.Enabled = false;
                    _frmAddItem1.txtType.Enabled = true;
                    _frmAddItem1.txtWeight.Enabled = false;
                    break;
                case "MONITOR":
                    _frmAddItem1.txtColor.Enabled = true;
                    _frmAddItem1.txtChipset.Enabled = false;
                    _frmAddItem1.txtGraphic.Enabled = false;
                    _frmAddItem1.txtHDD.Enabled = false;
                    _frmAddItem1.txtHeight.Enabled = false;
                    _frmAddItem1.txtMaterial.Enabled = false;
                    _frmAddItem1.txtOther.Enabled = true;
                    _frmAddItem1.txtPower.Enabled = false;
                    _frmAddItem1.txtRam.Enabled = false;
                    _frmAddItem1.txtShape.Enabled = false;
                    _frmAddItem1.txtSize.Enabled = true;
                    _frmAddItem1.txtType.Enabled = true;
                    _frmAddItem1.txtWeight.Enabled = false;
                    break;
                case "CPU":
                    _frmAddItem1.txtColor.Enabled = false;
                    _frmAddItem1.txtChipset.Enabled = true;
                    _frmAddItem1.txtGraphic.Enabled = true;
                    _frmAddItem1.txtHDD.Enabled = true;
                    _frmAddItem1.txtHeight.Enabled = true;
                    _frmAddItem1.txtMaterial.Enabled = true;
                    _frmAddItem1.txtOther.Enabled = true;
                    _frmAddItem1.txtPower.Enabled = true;
                    _frmAddItem1.txtRam.Enabled = true;
                    _frmAddItem1.txtShape.Enabled = false;
                    _frmAddItem1.txtSize.Enabled = false;
                    _frmAddItem1.txtType.Enabled = false;
                    _frmAddItem1.txtWeight.Enabled = false;
                    break;
                case "TELEVISION":
                    _frmAddItem1.txtColor.Enabled = true;
                    _frmAddItem1.txtChipset.Enabled = false;
                    _frmAddItem1.txtGraphic.Enabled = false;
                    _frmAddItem1.txtHDD.Enabled = false;
                    _frmAddItem1.txtHeight.Enabled = false;
                    _frmAddItem1.txtMaterial.Enabled = false;
                    _frmAddItem1.txtOther.Enabled = true;
                    _frmAddItem1.txtPower.Enabled = true;
                    _frmAddItem1.txtRam.Enabled = false;
                    _frmAddItem1.txtShape.Enabled = false;
                    _frmAddItem1.txtSize.Enabled = true;
                    _frmAddItem1.txtType.Enabled = true;
                    _frmAddItem1.txtWeight.Enabled = false;
                    break;
                case "AIR-CONDITIONER":
                    _frmAddItem1.txtColor.Enabled = true;
                    _frmAddItem1.txtChipset.Enabled = false;
                    _frmAddItem1.txtGraphic.Enabled = false;
                    _frmAddItem1.txtHDD.Enabled = false;
                    _frmAddItem1.txtHeight.Enabled = false;
                    _frmAddItem1.txtMaterial.Enabled = false;
                    _frmAddItem1.txtOther.Enabled = true;
                    _frmAddItem1.txtPower.Enabled = true;
                    _frmAddItem1.txtRam.Enabled = false;
                    _frmAddItem1.txtShape.Enabled = false;
                    _frmAddItem1.txtSize.Enabled = true;
                    _frmAddItem1.txtType.Enabled = false;
                    _frmAddItem1.txtWeight.Enabled = true;
                    break;
            }
        }
    }
}
